﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure
{
    public class SQLScriptGenerator
    {
        private static SQLScriptGenerator instance;
        public static SQLScriptGenerator Instance
        {
            get
            {
                if (instance == null)
                    instance = new SQLScriptGenerator();
                return instance;
            }
        }

        public string GenerateInsertSQL(object obj,string tableName)
        {
            Type type = obj.GetType();
            return string.Empty;

 
        }

        public string GenerateUpdateSQL(object obj,string tableName,List<string> keyColumns)
        {
            return string.Empty;
 
        }

        public string GenerateDeleteSQL(object obj,string tableName,List<string> keyColumns)
        {
            return string.Empty;
        }

        private string generateInsertSQL(string tableName, List<KeyValuePair<string, object>> columnValues)
        {
            string insertTemp = "Insert into {0} ({1}) Values ({2})";
            StringBuilder columnList = new StringBuilder();
            StringBuilder valueList = new StringBuilder();
            foreach (KeyValuePair<string, object> key in columnValues)
            {
                columnList.Append(addBracket(key.Key));
                columnList.Append(",");
                valueList.Append(addQuotation(key.Value));
                valueList.Append(",");
            }
            columnList.Remove(columnList.Length - 1, 1);
            valueList.Remove(valueList.Length - 1, 1);
            string columns = columnList.ToString();
            string values = valueList.ToString();
            string insert = string.Format(insertTemp, tableName, columns, values);
            return insert;
        }


        private string addBracket(string dbObj)
        {
            return "[" + dbObj + "]";
        }

        private string addQuotation(Object obj)
        {
            if (obj != null)
            {
                if (obj.GetType() == typeof(string) || obj.GetType() == typeof(DateTime) || obj.GetType() == typeof(Guid))
                    return "'" + obj.ToString() + "'";
                else
                    return obj.ToString();
            }
            else
                return "NULL";
        }


       
    }
}
